package com.kobako.controller;

import com.kobako.entity.EssayEntity;
import com.kobako.model.EssayDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * Created by kobako on 2017/4/12.
 * Just a game
 */
@Controller
public class BlogerController {
    @Autowired
    EssayDao blogDao;

    @RequestMapping("/blogerlogin")
    public String blogerLogin(HttpSession session){
        if(checkBloger(session)){
            return "/blogSubmit";
        }
        return "bloger/blogerLogin";
    }

    @RequestMapping(value = "/blogerLoginPost",method = RequestMethod.POST)
    public String blogerLoginPost(String name, String pass, HttpSession session){
        if(checkBloger(session)){
            return "index";
        }
        if(checkBlogerExist(name,pass)){
            session.setAttribute("bloger",Boolean.TRUE);
            return "forward:blogerSubmit";
        }
        return "index";
    }

    private boolean checkBlogerExist(String name,String pass){
        System.out.println("now check bloger exist! : name = "+name+" pass = "+pass);
        if("kobako".equals(name) && "qwer1234".equals(pass))return true;
        return false;
    }

    @RequestMapping("/blogerSubmit")
    public String blogerPostText(HttpSession session){
        if(checkBloger(session)){
            return "bloger/blogerSubmit";
        }else {
            return "index";
        }
    }

    private boolean checkBloger(HttpSession session){
        Object bloger = session.getAttribute("bloger");
        if(bloger != null && ! "".equals(bloger)){
            if(Boolean.TRUE.equals(bloger))return true;
        }
        return false;
    }

    @RequestMapping(value = "/submitEssay",method = RequestMethod.POST)
    public String submitEssay(@RequestParam String  title,@RequestParam String content,HttpSession session){
        if(checkBloger(session)) {
            EssayEntity essay = new EssayEntity();
            essay.setTitle(title);
            essay.setContent(content);

            blogDao.insertEssay(essay);
        }else{
            System.out.println("please login!");
        }
        return "index";
    }

    @RequestMapping(value = "/submitBlog",method = RequestMethod.POST)
    public String submitBlog(@RequestParam String  title,@RequestParam String content,HttpSession session){
        if(checkBloger(session)) {
            System.out.println(content);
            EssayEntity essay = new EssayEntity();
            essay.setTitle(title);
            essay.setContent(content);

            blogDao.insertBlog(essay);
        }else{
            System.out.println("please login!");
        }
        return "index";
    }
}
